package com.lin.blog.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.blog.constant.ArticleCategoryTagsConstant;
import com.lin.blog.constant.UserConstant;
import com.lin.blog.mapper.ArticleCategoryTagsMapper;
import com.lin.blog.mapper.CategoryMapper;
import com.lin.blog.service.IArticleCategoryTagsService;
import com.lin.blog.vo.ArticleCategoryTagsVO;
import com.lin.constant.ResultCode;
import com.lin.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lin
 * @since 2020-03-08
 */
@Service
@CacheConfig(cacheNames = "article")
public class ArticleCategoryTagsServiceImpl extends ServiceImpl<ArticleCategoryTagsMapper, ArticleCategoryTagsVO> implements IArticleCategoryTagsService {

    @Autowired
    private ArticleCategoryTagsMapper articleCategoryTagsMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 通过主键ID查询Article
     *
     * @param id
     * @return
     */
    @Override
    public Result getArticle(Long id) {
        // 参数不为空
        if (id == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }

        // 查询
        ArticleCategoryTagsVO article = articleCategoryTagsMapper.getArticleCategoryTagsVOById(id);

        // 不存在
        if (article == null) {
            return Result.failure(ResultCode.ARTICLE_NOT_EXIT);
        }

        return Result.success(article);
    }

    /**
     * 获取ArticleList 多条件查询
     * @param searchMap
     * @return
     */
    @Override
    public Result listArticle(Long pageNum,Long pageSize,Map<String, Object> searchMap) {


        // 分页
        Page<ArticleCategoryTagsVO> page = new Page(pageNum,pageSize);

        // role 是否为 包含 "ADMIN"
        if (searchMap.size()!=0 &&
                searchMap.get(UserConstant.ROLES)!=null) {
            List<String> roleList = (List<String>) searchMap.get(UserConstant.ROLES);
            if (roleList.contains(UserConstant.ROLE_ADMIN)){
                // 移除 只搜索 单作者的条件
                if (!StringUtils.isEmpty(searchMap.get(ArticleCategoryTagsConstant.AUTHOR))) {
                    searchMap.remove(ArticleCategoryTagsConstant.AUTHOR);
                }
            }
        }

        return Result.success(articleCategoryTagsMapper.listArticleCategoryTagsVOPage(page,searchMap));
    }





    /**
     * 获取ArticleList
     * @return
     */
    @Override
    public Result listArticle() {
        return Result.success(articleCategoryTagsMapper.listArticleCategoryTagsVO(null));
    }

    /**
     * 分页获取ArticleList 通过tagsId
     * @param tagsId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public Result  listArticleByTagsId(Long tagsId,Long pageNum, Long pageSize){
        Page page = new Page(pageNum, pageSize);
        Map<String, Long> map = new HashMap<>(15);
        if (-1 != tagsId) {
            map.put(ArticleCategoryTagsConstant.TAGSID,tagsId);
        }
        return Result.success(articleCategoryTagsMapper.listArticleCategoryTagsVOPage(page,map));
    }

    /**
     * 分页获取ArticleList 通过categoryId
     * @param categoryId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public Result  listArticleByCategoryId(Long categoryId,Long pageNum, Long pageSize){
        Page page = new Page(pageNum, pageSize);
        Map<String, List> map = new HashMap<>(15);
        if (-1 != categoryId) {
            List<Long> categoryIdByParentId = categoryMapper.getCategoryIdByParentId(categoryId);
            categoryIdByParentId.add(categoryId);
            map.put(ArticleCategoryTagsConstant.CATEGORY_List,categoryIdByParentId);
        }
        return Result.success(articleCategoryTagsMapper.listArticleCategoryTagsVOPage(page,map));
    }


    /**
     * 分页获取ArticleList
     * @return
     */
    @Override
    public Result listArticle(Long pageNum, Long pageSize) {
        Page page = new Page(pageNum, pageSize);
        return Result.success(articleCategoryTagsMapper.listArticleCategoryTagsVOPage(page,null));
    }



}
